package cn.smartravel.im.config;

import cn.smartravel.im.server.ChatServer;
import cn.smartravel.im.server.support.SmartHandshakeInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    @Bean
    public ChatServer chatServer() {
        return new ChatServer();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatServer(), "/chat")
                .addInterceptors(new SmartHandshakeInterceptor())
                .setAllowedOrigins("*");
    }
}
